package edu.gyc.test;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

class MyData {
    private volatile int n = 0;
    private AtomicInteger atomicInteger = new AtomicInteger(0);


    public void change38() {
        this.n = 38;
    }

    public void addPlusPlus() {
        n++;
    }

    public int getN() {
        return n;
    }

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
        atomicInteger.incrementAndGet();
    }
}


public class HiVolatile {
    public static void main(String[] args) throws InterruptedException {
        //poolAdd();
        // myVolatile();
        MyData data = new MyData();

        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(() -> {
                try {
                    // Thread.sleep(10);
                    for (int j = 0; j < 1000; j++) {
                        data.addPlusPlus();
                        data.setAtomicInteger();
                        // hiData.addPlusPlus();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(Thread.currentThread().getName() + "\t" + data.getN());
                }

            });
            t.start();

        }
//默认后台2个线程，main和gc垃圾回收线程
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + "\t" + data.getN());
        System.out.println(Thread.currentThread().getName() + "\t" + data.getAtomicInteger().get());
    }

    private static void poolAdd() {
        MyData data = new MyData();
        ExecutorService pools = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            pools.execute(() -> {
                try {
                    Thread.sleep(10);
                    for (int j = 0; j < 100; j++) {
                        data.addPlusPlus();
                    }


                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(Thread.currentThread().getName() + "\t" + data.getN());
                }

            });
        }
        pools.shutdown();
    }

    //volatile可以保证可见性，及时通知其他线程对象已被修改
    private static void myVolatile() {
        MyData myData = new MyData();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " come in.");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myData.change38();
            System.out.println(Thread.currentThread().getName() + " changed n=" + myData.getN());
        }).start();

        System.out.println(Thread.currentThread().getName() + " n==0 ,开始等待");
        while (myData.getN() == 0) {

        }
        System.out.println(Thread.currentThread().getName() + " game over.");
    }
}
